<html>
 <head>
  <title>Test Page for Workflows</title>
  <script language="javascript" src="../../jsunit/app/jsUnitCore.js"></script>
  <script language="javascript" src="../compiled/ca-all.js"></script>
 </head>
 <body>
  <script language="javascript">
    function testCreationWithMinimalConfig() {
	    var success = true;
		try{
			var workflow = new CA.Workflows.Workflow({
				"type" : "Test",
				name : "testCreationWithMinimalConfig",
				initial : "TestState",
				description : "Blah blah",
				attribute : "state"
			});
		}catch(e){
			success = false;
		}
		assertTrue("Should succeed",success);
    }
    function testCreationWithMinimalConfigAndRegister() {
	    var success = true;
		try{
			var workflow = new CA.Workflows.Workflow({
				"type" : "Test",
				name : "testCreationWithMinimalConfigAndRegister",
				initial : "TestState",
				description : "Blah blah",
				attribute : "state"
			},true);
		}catch(e){
			success = false;
		}
		assertTrue("Should succeed",success);
    }
    function testCreationWithFullConfig() {
	    var success = true;
		try{
			var workflow = new CA.Workflows.Workflow({
				"type" : "Test",
				name : "testCreationWithFullConfig",
				initial : "TestState",
				description : "Blah blah",
				content_types : ["ISimple","ISimple2"],
				attribute : "state",
				states : [
						{ name : "Beginning" },
						{ name : "Middle" },
						{ name : "Ending" } ],
				transitions : [
						{ name : "ToMiddle",
						  from_state : "Beginning",
						  to_state : "Middle"},
						{ name : "ToEnd",
						  from_state : "Middle",
						  to_state : "Ending"}
						  ]
			});
		}catch(e){
			success = false;
		}
		assertTrue("Should succeed",success);
    }
    function testCreationWithFullConfigAndRegister() {
	    var success = true;
		try{
			var workflow = new CA.Workflows.Workflow({
				"type" : "Test",
				name : "testCreationWithFullConfigAndRegister",
				initial : "TestState",
				description : "Blah blah",
				content_types : ["ISimple","ISimple2"],
				attribute : "state",
				states : [
						{ name : "Beginning" },
						{ name : "Middle" },
						{ name : "Ending" } ],
				transitions : [
						{ name : "ToMiddle",
						  from_state : "Beginning",
						  to_state : "Middle"},
						{ name : "ToEnd",
						  from_state : "Middle",
						  to_state : "Ending"}
						  ]
			},true);
		}catch(e){
			success = false;
		}
		assertTrue("Should succeed",success);
    }
    function testStateWithConfig() {
		var workflow = new CA.Workflows.Workflow({
			"type" : "Test",
			name : "testStateWithConfig",
			initial : "TestState",
			description : "Blah blah",
			content_types : ["ISimple","ISimple2"],
			attribute : "state",
			states : [
					{ name : "Beginning" },
					{ name : "Middle" },
					{ name : "Ending" } ]
		},true);
	    var success = true;
		try{
			var state = workflow.GetState("Middle");
			assertEquals("Name should match","Middle",state.name);
		}catch(e){
			success = false;
		}
		assertTrue("Should succeed",success);
    }
    function testTransitionWithConfig() {
		var workflow = new CA.Workflows.Workflow({
			"type" : "Test",
			name : "testTransitionWithConfig",
			initial : "TestState",
			description : "Blah blah",
			content_types : ["ISimple","ISimple2"],
			attribute : "state",
			states : [
					{ name : "Beginning" },
					{ name : "Middle" },
					{ name : "Ending" } ],
			transitions : [
					{ name : "ToMiddle",
					  from_state : "Beginning",
					  to_state : "Middle"},
					{ name : "ToEnd",
					  from_state : "Middle",
					  to_state : "Ending"}
					  ]
		},true);
	    var success = true;
		try{
			var transition = workflow.GetTransition("ToMiddle");
			assertEquals("Should match","ToMiddle",transition.name);
		}catch(e){
			success = false;
		}
		assertTrue("Should succeed",success);
    }
    function testSimpleTransition() {
	    var wf = new CA.Workflows.Workflow({ name : "testSimpleTransition",
    	                                     type : "TestTransition",
        	                                 description : "A Test Case",
            	                             initial : "Start",
                	                         attribute : "State" });
	    wf.AddState( { name : "Start",callback:function(c,w,i){c.started = true;} } );
    	wf.AddState( { name : "End",callback:function(c,w,i){c.ended = true;} } );
    	wf.AddTransition( { name : "finish",
        	                check : function(c,i){ c.checked=true;return true; },
            	            from_state : "Start",
                	        to_state : "End",
                    	    callback : function(c,w,i){ c.transitioned=true; } } );
    	wf.Register();
    	var o = {};
    	//transition by state
    	wf.Transition(o,"End");
    	assertTrue("Started",o.started);
    	assertTrue("Ended",o.ended);
    	assertTrue("Checked",o.checked);
    	assertTrue("Transitioned",o.transitioned);
    	o = {};
    	//transition by transition
    	wf.Transition(o,"finish");
    	assertTrue("Started",o.started);
    	assertTrue("Ended",o.ended);
    	assertTrue("Checked",o.checked);
    	assertTrue("Transitioned",o.transitioned);
    }
    function testSimpleTransitionWithCheck() {
	    var wf = new CA.Workflows.Workflow({ name : "testSimpleTransitionWithCheck",
    	                                     type : "TestTransitionWithCheck",
        	                                 description : "A Test Case",
            	                             initial : "Start",
                	                         attribute : "State" });
	    wf.AddState( { name : "Start",callback:function(c,w,i){c.started = true;} } );
    	wf.AddState( { name : "End",callback:function(c,w,i){c.ended = true;} } );
    	wf.AddTransition( { name : "finish",
        	                check : function(c,i){ return c.checked; },
            	            from_state : "Start",
                	        to_state : "End",
                    	    callback : function(c,w,i){ c.transitioned=true; } } );
    	wf.Register();
    	var o = {checked:false};
    	//transition by state
    	assertTrue("Fails to transition",!wf.Transition(o,"End"));
    	assertTrue("Started",o.started);
    	assertTrue("Ended",!o.ended);
    	assertTrue("Checked",!o.checked);
    	assertTrue("Transitioned",!o.transitioned);
    }
    function testInterfaces() {
    	CA.Interfaces.Create("IWorkflowTest",["NoOp"]);
		var workflow = new CA.Workflows.Workflow({
			"type" : "TestSuccess",
			name : "Workflow1",
			initial : "TestState",
			description : "Blah blah",
			content_types : ["IWorkflowTest"],
			attribute : "state",
			states : [
					{ name : "Beginning" },
					{ name : "Middle" },
					{ name : "Ending" } ],
			transitions : [
					{ name : "ToMiddle",
					  from_state : "Beginning",
					  to_state : "Middle"},
					{ name : "ToEnd",
					  from_state : "Middle",
					  to_state : "Ending"}
					  ]
		},true);
    	var x = {NoOp:function(){}};
    	assertEquals( "Should accept",CA.Workflows.FindWorkflow(x,"TestSuccess"),workflow );
    	assertNull("Should not find one",CA.Workflows.FindWorkflow({},"TestSuccess"));
    }
  </script>
 </body>
</html>